package com.dpiinc.TechPage.user.setting;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dpiinc.TechPage.BaseActivity;
import com.dpiinc.TechPage.LoginActivity;
import com.dpiinc.TechPage.R;
import com.dpiinc.TechPage.pen.dialog.AccountCancellationDialog;
import com.dpiinc.TechPage.room.RoomAiWriterDatabase;
import com.dpiinc.TechPage.user.CommonWebviewActivity;
import com.dpiinc.TechPage.user.utils.Constant;
import com.dpiinc.TechPage.user.utils.ToastUtils;
import com.beiji.lib.pen.PenManager;
import com.beiji.lib.pen.model.PenInfo;

import java.util.Locale;

import androidx.annotation.Nullable;

/**
 * Created by hu489_000 on 2018-06-25.
 */

public class SettingActivity extends BaseActivity {

    View user_agreement_layout;
    View logout_layout;
    View ai_pen_name_layout;
    TextView ai_pen_name;
    View setting_language_layout;
    TextView setting_language;

    PenInfo mPenInfo = PenManager.Companion.getInstance().getConnectedPen();
    private RelativeLayout accountCancellation;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.setting_main);
        super.onCreate(savedInstanceState);

        setBackWhite();
        setstatusBarColor(R.color.color_00bad2);

        ai_pen_name_layout = findViewById(R.id.ai_pen_name_layout);
        accountCancellation = findViewById(R.id.logout_account_cancellation);
        accountCancellation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AccountCancellationDialog dialog = new AccountCancellationDialog(SettingActivity.this);
                dialog.show();
            }
        });
        ai_pen_name_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mPenInfo == null){
                    ToastUtils.showTopTip(SettingActivity.this,getResources().getString(R.string.pls_connect_pen));
                    return;
                }
                startActivity(new Intent(SettingActivity.this, PenNameModifyActivity.class));
            }
        });

        setting_language_layout = findViewById(R.id.setting_language_layout);
        setting_language_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(SettingActivity.this, LanguageActivity.class));
            }
        });

        user_agreement_layout = findViewById(R.id.user_agreement_layout);
        user_agreement_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
          //      if("en".equals(getSharedPreferences("app_language",Context.MODE_PRIVATE).getString("lauType",Locale.getDefault().getLanguage()))){
                    CommonWebviewActivity.webviewEntrance(SettingActivity.this, Constant.PROTOCOL_AGREEMENT,"Agreement");
              /*  }else{
                    CommonWebviewActivity.webviewEntrance(SettingActivity.this, Constant.APP_AGREEMENT,"用户协议");
                }*/
            }
        });

        View user_agreement_layout2 = findViewById(R.id.user_agreement_layout2);
        user_agreement_layout2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               // if("en".equals(getSharedPreferences("app_language",Context.MODE_PRIVATE).getString("lauType",Locale.getDefault().getLanguage()))){
                    CommonWebviewActivity.webviewEntrance(SettingActivity.this, Constant.PROTOCOL_AGREEMENT,"Agreement");
           /*     }else{
                    CommonWebviewActivity.webviewEntrance(SettingActivity.this, Constant.APP_PROTOCOL,"隐私政策");
                }*/
            }
        });



        logout_layout= findViewById(R.id.logout_layout);
        logout_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                show1();
            }
        });

        ai_pen_name = findViewById(R.id.ai_pen_name);
        setting_language = findViewById(R.id.setting_language);
       /* if("zh".equals(getResources().getConfiguration().locale.getLanguage())){
            setting_language.setText(getResources().getString(R.string.setting_language_chinese));
        }else*/ if("en".equals(getResources().getConfiguration().locale.getLanguage())){
            setting_language.setText(getResources().getString(R.string.setting_language_english));
        }else if("es".equals(getResources().getConfiguration().locale.getLanguage())){
            setting_language.setText(getResources().getString(R.string.setting_language_spanish));
        }else if("fr".equals(getResources().getConfiguration().locale.getLanguage())){
            setting_language.setText(getResources().getString(R.string.setting_language_franch));
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        if(mPenInfo != null) {
            ai_pen_name.setText(mPenInfo.getName());
        }
    }

    private void show1() {
        final Dialog bottomDialog = new Dialog(this, R.style.BottomDialog);
        View contentView = LayoutInflater.from(this).inflate(R.layout.logout_pop, null);
        bottomDialog.setContentView(contentView);
        ViewGroup.LayoutParams layoutParams = contentView.getLayoutParams();
        layoutParams.width = getResources().getDisplayMetrics().widthPixels;
        contentView.setLayoutParams(layoutParams);
        bottomDialog.getWindow().setGravity(Gravity.BOTTOM);
        bottomDialog.show();

        Button pop_logout = (Button) contentView.findViewById(R.id.pop_logout);
        pop_logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                bottomDialog.cancel();

                RoomAiWriterDatabase db = RoomAiWriterDatabase.getInstance(SettingActivity.this);
                db.userDao().clearTable();
                db.noteDao().deleteAll();
                Intent intent = new Intent(SettingActivity.this, LoginActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);

            }
        });


        Button cancel = (Button) contentView.findViewById(R.id.pop_cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bottomDialog.cancel();
            }
        });
    }
}
